package vcp.gui.forms;

import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



import java.awt.Color;


import java.awt.EventQueue;

import vcp.gui.components.VCPFrame;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.validators.NameValidator;
import vcp.gui.forms.validators.PasswordValidator;
import vcp.gui.frames.SecureFrame;
import vcp.gui.test.entities.User;



public class LoginForm extends VCPPanel implements ActionListener {
	private JLabel lblLoginError;
	private JTextField textUsername;
	private JPasswordField txtPassword;
	private JButton btnLogin;
	

	/**
	 * Create the panel.
	 */
	public LoginForm() {
		lblLoginError = new JLabel("");
		lblLoginError.setForeground(Color.RED);
		
		JLabel lblUname = new JLabel("Username");
		
		textUsername = new JTextField();
		textUsername.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		
		txtPassword = new JPasswordField();
		txtPassword.setColumns(10);
		
		btnLogin = new JButton("login");
		btnLogin.addActionListener(this);
		btnLogin.setActionCommand("btnLogin");
		
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
							.addComponent(btnLogin)
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblUname)
									.addComponent(lblPassword))
								.addGap(33)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(txtPassword)
									.addComponent(textUsername, GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))))
						.addComponent(lblLoginError))
					.addContainerGap(100, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(lblLoginError)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUname)
						.addComponent(textUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnLogin)
					.addContainerGap(22, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ((e.getActionCommand()).equals("btnLogin")) {
			System.out.println("login pressed");
			
			/*
			// user that logs in ...
			
		    
			// .. login with gui 
			NameValidator svalidator = new NameValidator();
			PasswordValidator pvalidator = new PasswordValidator();
			String username = textUsername.getText();
			String password = txtPassword.getText(); 
			if(!svalidator.validate(username) || !pvalidator.validate(password)){
				lblLoginError.setText("Wrong fields ...");
			}else{
				// check username + password 
			
				// debug test initialization
				// start in thread .. demo 
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							VCPFrame sframe = new SecureFrame(user);
							sframe.setVisible(true);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});			
                // kill old .. need manager to keep occurence
                 * 
                 */
			
			
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						User user = new User(1,"max","12345","customerService");	
						VCPFrame sframe = new SecureFrame(user);
						sframe.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});		
			    VCPFrame gframe = getParentFrame();			    
			    gframe.die();
			}	
		
		
		}		
	}






